%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <signal.h>

extern numLigne ;
extern nbC;
extern argument;
int colTraite=0;
FILE* f;
void yyerror(char *str) ; 
int yywrap() ;
%}


%union{char cval[1000];
	int ival;
	float fval;}

%token  <ival> NOMBRE
%token  <cval> MOT BLC
%token  <cval> BF 
%token  <cval>  LABEL
%token <fval>  REEL
%token  <cval> DEBUT_TABLE
%token  <cval> DEBUT_CENTER
%token  <cval> DEBUT_TABULAR
%token  <cval> FIN_TABULAR
%token  <cval> FIN_CENTER
%token  <cval> FIN_TABLE
%token  <cval> MULTICOLUMN
%token  <cval> DOUBLE_BS
%token  <cval> ETOILE
%token  <cval> VIRGULE
%token  <cval> HLINE
%token  <cval> TABULATION
%token  <cval> ET
%token  <cval> AO
%token  <cval> AF
%token  <cval> COLLSPAN
%token  <cval> BS
%token  <cval> DOLLAR DP
%token  <cval> CAPTION
%type   <cval> Corps tableau ligne_tableau trash Case Legende_texte Texte Legende


%start my
%%
file: trash Corps trash
      |/*rien*/
;
my:  DEBUT_TABULAR TABULATION  trash  {}
|BLC DEBUT_TABULAR TABULATION  BLC
;


Corps: DEBUT_TABULAR TABULATION  trash tableau FIN_TABULAR trash {printf("reconnu\n");}
| DEBUT_TABULAR TABULATION  tableau FIN_TABULAR Legende Label trash{char expression[4000]="";
								    strcat(expression,"<html>\n\t<body>\n\t\t<center>\n\t\t<table border cellspacing=0>");
								    strcat(expression,$3);
								    strcat(expression,"\n\t</table>");
								    strcat(expression,$5);
								    strcat(expression,"\n\t\t</center>\n\t</body>\n</html>");
								    fprintf(f,"%s",expression);
								    fclose(f);}

      |DEBUT_TABULAR TABULATION tableau FIN_TABULAR trash           {char expression[4000]="";
								    strcat(expression,"<html>\n\t<body>\n\t\t<table border cellspacing=0>");
								    strcat(expression,$3);
								    strcat(expression,"\n\t</table>");
								    strcat(expression,"\n\t</body>\n</html>");
								    fprintf(f,"%s",expression);
							    	    fclose(f);}
;
tableau: HLINE ligne_tableau tableau HLINE{char expression[4000]="";
					   strcat(expression,"\n\t\t\t<tr>");
		                           strcat(expression,$2);
		                           strcat(expression,"\n\t\t\t</tr>");
		                           strcat(expression,$3);
					   strcpy($$,expression);
					}
	|HLINE ligne_tableau HLINE  {char expression[4000]="";	
				            strcat(expression,"\n\t\t\t<tr>");
				             strcat(expression,$2);
				             strcat(expression,"\n\t\t\t</tr>");
					     strcpy($$,expression);}


;

ligne_tableau:Case ET ligne_tableau {char expression[4000]="";
			   			strcat(expression,"\n\t\t\t\t<td ");
						switch(((char*)argument)[colTraite%nbC]){
							case 'r':
									 strcat(expression,"align=right>");
									 break;
							case 'c':
									 strcat(expression,"align=center>");
									 break;
							case 'l':
									strcat(expression,"align=left>");
									 break;
							
						}
                        		    strcat(expression,$1);
					    strcat(expression,"</td>");
					    strcat(expression,$3);
					    strcpy($$,expression);
						colTraite++;}

	      |Case DOUBLE_BS         {char expression[4000]="";
			   			strcat(expression,"\n\t\t\t\t<td ");
						switch(((char*)argument)[colTraite%nbC]){
							case 'r':
									 strcat(expression,"align=right>");
									 break;
							case 'c':
									 strcat(expression,"align=center>");
									 break;
							case 'l':
									strcat(expression,"align=left>");
									 break;
							
						}
                        		    strcat(expression,$1);
					    strcat(expression,"</td>");
					    strcpy($$,expression);
						colTraite++;}   

	      |ET ligne_tableau         {char expression[4000]="";
		     			strcat(expression,"\n\t\t\t\t<td></td>");
		     			strcat(expression,$2);
		     			strcpy($$,expression);
		     			colTraite++;}

	      |MULTICOLUMN AO NOMBRE AF TABULATION AO Case AF ET ligne_tableau {char expression[4000]="";
			    strcat(expression,"\n\t\t\t\t<td colspan=\"");
                            char nb[3];
			    sprintf(nb,"%d",$3);
			    strcat(expression,nb);
			    strcat(expression,"\"");
			    char * pch;
			    if((pch=strchr($5,'c'))!=NULL){
				strcat(expression," align=center>");
			    }else if((pch=strchr($5,'l'))!=NULL){
				strcat(expression," align=left>");
		   	    }else if((pch=strchr($5,'r'))!=NULL){
				strcat(expression," align=right>");
			    }
			     strcat(expression, $7);
			    strcat(expression,"</td>");
			    strcat(expression,$9);
			    strcpy($$,expression);
			     colTraite+=$3;}
	   |MULTICOLUMN AO NOMBRE AF TABULATION AO Case AF DOUBLE_BS {char expression[4000]="";
			    strcat(expression,"\n\t\t\t\t<td colspan=\"");
                            char nb[3];
			    sprintf(nb,"%d",$3);
			    strcat(expression,nb);
			    strcat(expression,"\"");
			    char * pch;
			    if((pch=strchr($5,'c'))!=NULL){
				strcat(expression," align=center>");
			    }else if((pch=strchr($5,'l'))!=NULL){
				strcat(expression," align=left>");
		   	    }else if((pch=strchr($5,'r'))!=NULL){
				strcat(expression," align=right>");
			    }
			    strcat(expression,$7);
			    strcat(expression,"</td>");
			    strcpy($$,expression);
			    colTraite+=$3;}
;
Case:Texte              {strcpy($$,$1);}
  
;				
Texte:	 MOT {strcpy($$,$1);}
	|REEL	{sprintf($$,"%f",$1);}
	|NOMBRE {sprintf($$,"%d",$1);}
	|DOLLAR Texte DOLLAR {strcpy($$,$2);}
        |BF AO Texte AF	{char expression[4000];
			strcat(expression,"<b>");
			strcat(expression,$3);
			strcat(expression,"</b>");
			strcpy($$,expression);			 
			}
	

;
Legende_texte :
	Case {strcpy($$,$1);}
	|Case Legende_texte {char expression[4000]="";
				 strcat(expression,$1);
				 strcat(expression,$2);
				 strcpy($$,expression);}
;

Legende:
	CAPTION AO Legende_texte AF { char expression[4000]="";
				       strcat(expression,"\n\t\t<p>");
			               strcat(expression,$3);
				       strcat(expression,"</p>");

				       strcpy($$,expression);} 
Label: LABEL AO Label_texte AF {printf("\nlabel reconnu");}
;

Label_texte:
MOT DP MOT {}
;
trash:
	 MOT {}
	|NOMBRE {}
	|REEL {}
	|AO {}
	|AF {}
	|BS {}
	|ETOILE {}
	|VIRGULE {}
	|DOLLAR
	|DOLLAR trash
	|VIRGULE trash {}
	|BS trash {}
	|ETOILE trash {}
	|AO trash {}
	|AF trash {}
	|MOT trash {}
	|NOMBRE trash {}
	|REEL trash {}    
;

%%

extern  FILE * yyin;
int yywrap()
{
    return 1 ;
}

void yyerror(char *str)
{
        fprintf(stderr,"Erreur: %s\n",str);
}

int main(int argc, char *argv[])
{
        printf("[CTRL-D pour finir]\n");
	f=fopen("tableau.html","w");

	if (argc<=1)
	  {fprintf(stderr,"fichier entree a mettre en param");return 0 ;
	  }
	else{
	char * chn;
	//	printf("fichier a prendre=%s/n",argv[1]);
	//printf("a");
	//	yyin=fopen("exemple.tex","r");
	yyin=fopen(argv[1],"r");
        yyparse();
	fclose(yyin);
	printf("\n");
        printf("Fin d'exécution.\n") ;
	return 0;
	}
}


